package com.example.tp1;


import android.util.Log;

public class BoardField{
	
	private float squareSize_;
	private float top_;
	//private float right_;
	private float left_;
	private int letter_;
	private int number_;
	private String mSquareLocation;
	final static private String squareId[][] = {{"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7"}, {"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7"},
			{"c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7"}, {"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7"}, {"e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"},
			{"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7"}, {"g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7"}, {"h0", "h1", "h2", "h3", "h4", "h5", "h6", "h7"}};

	public BoardField(float top, float left, float right)
	{
		squareSize_ = (right-left)/8;
		top_ = top;
		//right_ = right;
		left_ = left;
	}
	
	public String[] setSquareLine(float lastX, float lastY, float x, float y)
	{
		//Log.i("locFirst", String.valueOf(lastX) + "," + String.valueOf(lastY));
		String[] listSquareLine = new String[4];
		squareLocation(lastX, lastY);
		final float dx = x - lastX;
		final float dy = y - lastY;
		int countArray = 0;
		if (dy>0 && Math.abs(dy)>Math.abs(dx))
		{
			if (number_ < 7)
			{
				for (int i = -2; i < 3; i++)
				{
					if ((letter_+i) >= 0 && (letter_+i) <= 7 && i != 0)
					{
						listSquareLine[countArray] = squareId[letter_+i][number_+1];
						countArray++;
					}
				}
			}
		}
		else if (dy<0 && Math.abs(dy)>Math.abs(dx))
		{
			if (number_ > 0)
			{
				for (int i = -2; i < 3; i++)
				{
					if (letter_+i >= 0 && letter_+i <= 7 && i != 0)
					{
						listSquareLine[countArray] = squareId[letter_+i][number_-1];
						countArray++;
					}
				}
			}
		}
		else if (dx>0 && Math.abs(dx)>Math.abs(dy))
		{
			if (letter_ < 7)
			{
				for (int i = -2; i < 3; i++)
				{
					if (number_+i >= 0 && number_+i <= 7 && i != 0)
					{
						listSquareLine[countArray] = squareId[letter_+1][number_+i];
						countArray++;
					}
				}
			}
		}
		else if (dx<0 && Math.abs(dx)>Math.abs(dy))
		{
			if (letter_ > 0)
			{
				for (int i = -2; i < 3; i++)
				{
					if (number_+i >= 0 && number_+i <= 7 && i != 0)
					{
						listSquareLine[countArray] = squareId[letter_-1][number_+i];
						countArray++;
					}
				}
			}
		}
		//Log.i ("square", listSquareLine[0] + ", " + listSquareLine[1] + ", " + listSquareLine[2] + listSquareLine[3]);
		String[] listSquareLineFinal = new String[countArray];
		for (int i = 0; i < countArray; i++)
		{
			listSquareLineFinal[i] = listSquareLine[i];
		}
		return listSquareLineFinal;
	}
	
	private void squareLocation(float x, float y)
	{
		
		long number = (long)((y-top_)/squareSize_);
		number_ = (int)number;		
		long letter = (long)((x-left_)/squareSize_);
		letter_ = (int)letter;
		Log.i("loc", String.valueOf(letter_) + ", " + String.valueOf(number_));
		mSquareLocation = squareId[letter_][number_];
	}
	
	public String getSquareLoc()
	{
		return mSquareLocation;
	}

	

}
